var path=require("path")
var fs=require("fs")
module.exports={
    deleteFolder : function(path) {
        var files = [];
        if (fs.existsSync(path)) {
            files = fs.readdirSync(path);
            files.forEach(function (file, index) {
                var curPath = path + "/" + file;
                if (fs.statSync(curPath).isDirectory()) { // recurse
                    this.deleteFolder(curPath);
                } else { // delete file
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(path);
        }
    },
    mkDeepDir : function(dir) {
        var pathinfo = path.parse(dir)
        if (!fs.existsSync(pathinfo.dir)) {
            this.mkDeepDir(pathinfo.dir)
        }
        if(!fs.existsSync(dir)){
            try{
                fs.mkdirSync(dir)
            }catch(e){
                return false;
            }
        }
    },
    getFileMd5:async function(path){
        return new Promise(function(resole){
            var fs = require('fs');

            if(!fs.existsSync(path))
                resole(null)

            var crypto = require('crypto');

            var start = new Date().getTime();
            var md5sum = crypto.createHash('md5');
            var stream = fs.createReadStream(path);
            stream.on('data', function(chunk) {
                md5sum.update(chunk);
            });
            stream.on('end', function() {
                str = md5sum.digest('hex').toUpperCase();
                resole(str);
                console.log('文件:'+path+',MD5签名为:'+str+'.耗时:'+(new Date().getTime()-start)/1000.00+"秒");
            });
        })
    },
    mkdir:function(dirpath, mode, callback, position) {
        // Split all directories
        var parts = path.normalize(dirpath).split('/');
        // If the first part is empty then remove this part
        if (parts[0] == "")
            parts = parts.slice(1);
    
        // Set the initial configuration
        mode = mode || 0777;
        position = position || 0;
    
        // Check se current position is greater than the list of folders
        if (position > parts.length) {
            // If isset the callback then it will be invoked
            if (callback)
                callback();
            // Exit and return a positive value
            return true;
        }
    
        // Build the directory path
        var directory = (dirpath.charAt(0) == '/' ? '/' : '') + parts.slice(0, position + 1).join('/');
    
        // Check if directory exists
        if (fs.existsSync(directory)) {
            module.exports.mkdir(dirpath, mode, callback, position + 1);
        } else {
            if (fs.mkdirSync(directory, mode)) {
                // If isset the callback then it will be invoked
                if (callback)
                    callback(errors.renderError('mkdir', directory));
                // Send the new exception
                throw errors.renderError('mkdir', directory);
            } else {
                module.exports.mkdir(dirpath, mode, callback, position + 1);
            }
        }
    }
}